import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * <KEYIN> – Mapper 的键输入类型 ：LongWritable（偏移量）
 * <VALUEIN> – Mapper 的值输入类型 ：Text
 *
 * <KEYOUT> – Mapper 的关键输出类型：Text
 * <VALUEOUT> – Mapper 的值输出类型：IntWritable
 */
public class WordMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    Text k = new Text();

    IntWritable v = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        // 1 获取一行 先从Text对象转成String对象
        String line = value.toString();
        // 2 切割
        String[] words = line.split(" ");
        // 3 输出 输出前先从String对象转成Text对象 因为每读一行就走一遍map方法，为了防止占用内存过大，吧Text放在成员变量上
        for (String word : words) {
            k.set(word);
            context.write(k, v);
        }
    }
}
